/**
 * Created by gaoyj on 2018/6/28.
 */
window.onload = function (ev) {
    topbar.hide();
};
//销卡模板
var str;
$.post("productPrice/findModelPrice", null, function (data) {
    var group_obj = data;
    for (var i = 0; i < group_obj.length; i++) {
        str += '<option value="' + group_obj[i].id + '">' + group_obj[i].modelName + '</option>'
    }
    $("#supplyModel").append(str);
});
$("#quickSetting").validate({
    rules: {
        minPrice: {
            required: true,
            range: [0.5, 1.0]
        }, maxPrice: {
            required: true,
            range: [0.5, 1.0]
        }

    }
});

function reloadDiscount(obj) {
    if ($(obj).val() == '1' || $(obj).val() == '0') {
        $("#minPrice").removeAttr("readonly", "readonly")
        $("#maxPrice").removeAttr("readonly", "readonly")
    } else {
        $("#minPrice").attr("readonly", "readonly");
        $("#maxPrice").attr("readonly", "readonly");
    }
}

$("#formSubmit").click(function () {
    if ($('input[name=userIds]:checked').length < 1&&$("#supplyModel").val()=="") {
        layer.msg("请选择销卡模板或会员");
        return ;
    }
    if ($('input[name=userIds]:checked').length >= 1&&$("#supplyModel").val()!="") {
        layer.msg("销卡模板和会员不可同时选择");
        return ;
    }
    if ($('input[name=productTypes]:checked').length < 1) {
        layer.msg("请选择运营商");
        return;
    }
    if ($("#method").val() ==0||$("#method").val() ==1) {
        var v = $("#quickSetting").valid();
        if (!v) {
            return;
        }
        if($("#minPrice").val()>$("#maxPrice").val()){
            layer.msg("最大折扣不可小于最低折扣");
            return;
        }
    }
    $.confirm({
        title: '确认!',
        content: '确定提交吗？',
        confirm: function () {
            submitForm();
        }
    })

});


function submitForm() {
    $.post("/productPrice/setUpWorkNumberPrice", $("#quickSetting").serialize(), function (data) {
        layer.msg(data.message);
    }).error(function (e) {
        layer.msg(e);
    });
}

$("#searchUsers").click(function () {
    $.post("/productPrice/queryUserByAccount", {userAccount: $.trim($("#userAccount").val())}, function (data) {
        if (data == null || data.length == 0) {
            layer.msg("请输入正确会员编号");
            return;
        }
        var str = '';
        for (var i = 0; i < data.length; i++) {
            str += '<label class="icheck" style="margin-right:10px;">' +
                '<input name="userIds" type="checkbox" value="' + data[i].userId + '">' + data[i].supInfo.name + '</label>';
        }
        $("#userSet").html(str);
        // $("#userSet .i-checks").iCheck({
        //     checkboxClass: "icheckbox_square-green",
        //     radioClass: "iradio_square-green",
        // })

    });
})